package com.beaut.project.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.beaut.project.dao.SMSMapper;
import com.beaut.project.util.SMSUtil;
import com.beaut.project.util.Utility;

@Transactional(rollbackFor = Exception.class)
@Service("smsService")
public class SMSService {
	@Autowired
	private SMSMapper smsMapper;
	public String listPageSMS(Map<String,Object> map){
		List<Map<String,Object>> list= smsMapper.listPageSMS(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageSMSNoSend(Map<String,Object> map){
		List<Map<String,Object>> list= smsMapper.listPageSMSNoSend(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String hasNoReadSMS(Map<String,Object> map){
		int count = smsMapper.hasNoReadSMS(map);
		return String.valueOf(count);
	}
	public String getSMS(Map<String,Object> map){
		Map<String,Object> map2= smsMapper.getSMS(map);
		map2.put("sms_module", SMSUtil.getSMSModuleNameByKey(Integer.valueOf(map2.get("sms_module").toString())));
		return Utility.toJson(map2);
	}
	public String updateSMSSend(Map<String,Object> map){
		List<String> list = Arrays.asList(String.valueOf(map.get("ids")).split(","));
		map.put("send_time", Utility.getCurDateTimeStr());
		map.put("list", list);
		smsMapper.updateSMSSend(map);
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String updateSMSRead(Map<String,Object> map){
		List<String> list = Arrays.asList(String.valueOf(map.get("ids")).split(","));
		map.put("list", list);
		map.put("read_time", Utility.getCurDateTimeStr());
		smsMapper.updateSMSRead(map);
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String deleteSMS(Map<String,Object> map){
		List<String> list = Arrays.asList(String.valueOf(map.get("ids")).split(","));
		
		smsMapper.deleteSMS(list);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	
	public String addSMS(Map<String,Object> map){
		smsMapper.addSMS(map);
		return Utility.rtJson(Utility.RETURN_OK, "插入成功", null);
	}
	public String getSMSModule(){
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		Set<Entry<Integer, String>>  set = SMSUtil.smsModuleMap.entrySet();
		for(Entry<Integer, String> e : set){
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("id", e.getKey());
			map.put("value", e.getValue());
			list.add(map);
		}
		return Utility.lstMaptoJson(list);
	}
}
